var router = require("koa-router")();
const limit = 916.1538461538462

router.post("/", async (ctx) => {
    // console.log(ctx.request.body)
    let y = -1
    let x1 = parseFloat(ctx.request.body.data1)
    let x2 = parseFloat(ctx.request.body.data2)
    let x3 = parseFloat(ctx.request.body.data3)
    let x4 = parseFloat(ctx.request.body.data4)
    let x5 = parseFloat(ctx.request.body.data5)
    let x6 = parseFloat(ctx.request.body.data6)
    let x7 = parseFloat(ctx.request.body.data7)
    let x8 = parseFloat(ctx.request.body.data8)
    let x9 = parseFloat(ctx.request.body.data9)
    let x10 = parseFloat(ctx.request.body.data10)
    let avg1 = (x1 + x2 + x3 + x4 + x5) / 5
    let avg2 = (x6 + x7 + x8 + x9 + x10) / 5
    if (avg1 < avg2) {
        if (avg2 < limit)
            y = -7.91 + 0.013 * avg2
        else if (avg1 > limit)
            y = 0.856 + 131.9 * (0.996 ^ avg2)
        else {
            if((limit - avg1) > (avg2 - limit))
                y = -7.91 + 0.013 * limit
            else
                y = 0.856 + 131.9 * 0.996 ^ avg2
        }
    } else if (avg1 > avg2)
        y = 3.2 * (10 ^ ((-12) * (avg2 ^ 4))) - 1.36 * (10 ^ ((-8) * (avg2 ^ 3))) + 2.1 * (10 ^ ((-5) * (avg2 ^ 2))) - 0.01 * avg2 + 3.07
    let info = "normal"
    if (y >= 4)
        info = "warming"
    else if (y === -1)
        info = "error"
    ctx.body = {
        "y": y,
        "info": info
    }

});

module.exports = router.routes();
